/**
 * Revisão: Skubs em novembro de 2008 
 */

// Global object to hold drag information.
var dragObj = new Object();

function dragStart(event, id) {
    var el;
    var x, y;

    // If an element id was given, find it. Otherwise use the element being
    // clicked on.

	var div = createAuxiliaryDiv();
	dragObj.divNode = div;

    if (id) {
    	dragObj.elNode = document.getElementById('block_' + id);
    } else {
    	if (browser.isIE)
	        dragObj.elNode = window.event.srcElement;
	    if (browser.isNS)
     		dragObj.elNode = event.target;

    	// If this is a text node, use its parent element.
	    if (dragObj.elNode.nodeType == 3)
    	    dragObj.elNode = dragObj.elNode.parentNode;
  	}
  
  	// Get cursor position with respect to the page.
  	if (browser.isIE) {
    	x = window.event.clientX + document.documentElement.scrollLeft
      		+ document.body.scrollLeft;
    	y = window.event.clientY + document.documentElement.scrollTop
      		+ document.body.scrollTop;
  	}
  
  	if (browser.isNS) {
    	x = event.clientX + window.scrollX;
    	y = event.clientY + window.scrollY;
  	}

  	// Save starting positions of cursor and element.
	dragObj.cursorStartX = x;
  	dragObj.cursorStartY = y;
  	dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  	dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  	if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  	// Update element's z-index.
  	dragObj.elNode.style.zIndex = ++globalZIndex;
  	dragObj.divNode.style.zIndex = ++globalZIndex;
	
	document.body.appendChild(dragObj.divNode);
	
  	// Capture mousemove and mouseup events on the page.
  	if (browser.isIE) {
    	document.attachEvent("onmousemove", dragGo);
    	document.attachEvent("onmouseup",   dragStop);
	    window.event.cancelBubble = true;
    	window.event.returnValue = false;
	}
  
  	if (browser.isNS) {
    	document.addEventListener("mousemove", dragGo,   true);
    	document.addEventListener("mouseup",   dragStop, true);
    	event.preventDefault();
	}
}

function dragGo(event) {
  	var x, y;
  	var threshold = 2;
  	var newX, newY, oldX, oldY;
  	var delta;
	var MIN_WIDTH, MIN_HEIGHT;
	MIN_X = 0; // Herda um padding 2px do frame
  	if (browser.isIE) {
		MIN_Y = 40;
	}
  	if (browser.isNS) {
		MIN_Y = 36; // É preciso descontar o espaço do workspace
	}

  	// Get cursor position with respect to the page.
  	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
      		+ document.body.scrollLeft;
    	y = window.event.clientY + document.documentElement.scrollTop
      		+ document.body.scrollTop;
  	}
  
  	if (browser.isNS) {
    	x = event.clientX + window.scrollX;
    	y = event.clientY + window.scrollY;
  	}

  	// Move drag element by the same amount the cursor has moved.
  	newX = (dragObj.elStartLeft + x - dragObj.cursorStartX);
  	newY = (dragObj.elStartTop  + y - dragObj.cursorStartY);
  	oldX = parseInt(dragObj.elNode.style.left.replace('px',''), 10);
  	oldY = parseInt(dragObj.elNode.style.top.replace('px',''), 10);
    
  	if(newX >= MIN_X && Math.abs(oldX - newX) >= threshold) {
  		delta = Math.abs(oldX - newX);
		delta = delta / threshold;
		delta = Math.floor(delta) * threshold;

		// Reajusta		
    	if(newX > oldX) {
	  		dragObj.elNode.style.left = (oldX + delta) + "px";
		} else {
	  		dragObj.elNode.style.left = (oldX - delta) + "px";
		}
		dragObj.divNode.style.left = x;		
  	}
  
  	if(newY >= MIN_Y && Math.abs(oldY - newY) >= threshold) {
  
  		delta = Math.abs(oldY - newY);
		delta = delta / threshold;
		delta = Math.floor(delta) * threshold;
		
		// Reajusta	  
		if(newY > oldY)	{
	  		dragObj.elNode.style.top  = (oldY + delta) + "px";
		} else {
	  		dragObj.elNode.style.top  = (oldY - delta) + "px";
		}
		dragObj.divNode.style.top = y;
  	}

  	if (browser.isIE) {
    	window.event.cancelBubble = true;
    	window.event.returnValue = false;
  	}

  	if (browser.isNS) {
    	event.preventDefault();
    }
}

function dragStop(event) {
  	// Stop capturing mousemove and mouseup events.  

	var el = dragObj.elNode;
	var block_id = el.id.replace('block_', '');
	var block_x = el.style.left.replace('px','');
	var block_y = el.style.top.replace('px','');
	document.body.removeChild(dragObj.divNode);
	delete(dragObj.divNode);

	/**
	 * SKUBS AJAX
	 */
//	AjaxFunctions.updateLocation(block_id, block_x, block_y, null);

	if (browser.isIE) {
    	document.detachEvent("onmousemove", dragGo);
    	document.detachEvent("onmouseup",   dragStop);
  	}
  	if (browser.isNS) {
    	document.removeEventListener("mousemove", dragGo,   true);
    	document.removeEventListener("mouseup",   dragStop, true);
  	}
}
  
